﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Table = FCNS.Data.Table;
using DoubleH.Utility;
using DoubleH.Utility.Configuration;
using System.Windows.Interop;
using System.Collections;
using System.Diagnostics;

namespace POS
{
    public partial class MainWindow : Window
    {
        internal enum EnumUI
        {
            登陆,
            零售,
            结账
        }

        internal void ChangeUI(EnumUI ui)
        {
            switch (ui)
            {
                case EnumUI.零售:
                    pSale.NewOrder();
                    frame1.Navigate(pSale);
                    break;

                case EnumUI.结账:
                    frame1.Navigate(pPay);
                    break;

                default:
                    frame1.Navigate(pLogin);
                    break;
            }
        }

        #region 按键事件
        private void InitKeyDown()
        {
            IntPtr handle = new WindowInteropHelper(this).Handle;
            WindowsUtility.RegisterHotKey(handle, 0x9901, System.Windows.Forms.Keys.F1);
            WindowsUtility.RegisterHotKey(handle, 0x9902, System.Windows.Forms.Keys.F2);
            WindowsUtility.RegisterHotKey(handle, 0x9903, System.Windows.Forms.Keys.F3);
            WindowsUtility.RegisterHotKey(handle, 0x9904, System.Windows.Forms.Keys.F4);
            WindowsUtility.RegisterHotKey(handle, 0x9905, System.Windows.Forms.Keys.F5);
            WindowsUtility.RegisterHotKey(handle, 0x9906, System.Windows.Forms.Keys.F6);
            WindowsUtility.RegisterHotKey(handle, 0x9909, System.Windows.Forms.Keys.F9);
            WindowsUtility.RegisterHotKey(handle, 0x9910, System.Windows.Forms.Keys.F10);

            HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
            source.AddHook(WndProc);
        }

        private void UnInitKeyDown()
        {
            IntPtr handle = new WindowInteropHelper(this).Handle;
            WindowsUtility.UnregisterHotKey(handle, 0x9901);
            WindowsUtility.UnregisterHotKey(handle, 0x9902);
            WindowsUtility.UnregisterHotKey(handle, 0x9903);
            WindowsUtility.UnregisterHotKey(handle, 0x9904);
            WindowsUtility.UnregisterHotKey(handle, 0x9905);
            WindowsUtility.UnregisterHotKey(handle, 0x9906);
            WindowsUtility.UnregisterHotKey(handle, 0x9909);
            WindowsUtility.UnregisterHotKey(handle, 0x9910);
        }

        private void KeyDowned(int key)
        {
            if (!this.IsActive)
                return;

            switch (key)
            {
                case 0x9910:
                    PosConfig();
                    break;

                case 0x9909:
                    JiaoBan();
                    break;

                case 0x9906:
                    OpenCashbox();
                    break;

                case 0x9905:
                    GuaDan();
                    break;

                case 0x9904:
                    NewOrder();
                    break;

                case 0x9903:
                    LogOff(true);
                    break;

                case 0x9902:
                    CloseThis();
                    break;

                case 0x9901:
                    ShowHelp();
                    break;
            }
        }

        private void JiaoBan()
        {
            if (!App.SaleMode|| Table.UserS.LoginUser == null)
                return;
            if (App.CurrentShift.UserSId != Table.UserS.LoginUser.Id)
            {
                MessageWindow.Show("当前班次属于 " + App.CurrentShift._UserSName + " 请重新登陆.");
                return;
            }
            if (MessageWindow.Show("", "确认交班吗?", MessageBoxButton.YesNo) == MessageBoxResult.No)
                return;

            JiaoBanWindow jb = new JiaoBanWindow();
            jb.Init(false,false);
            jb.ShowDialog();
            if (jb.Ensure)
            {
                if (DoubleHConfig.AppConfig.PosIsPrintJiaoBan)
                {
                    PrintFunction.PrintPosJiaoBan(Table.UserS.LoginUser._LoginName, App.CurrentShift.PosNO, jb.doubleUpDownMoney.Value.Value.ToString(), jb.labelMoneySum.Content.ToString());
                }
                LogOff(false);
            }
        }

        private void PosConfig()
        {
            if (Table.UserS.LoginUser == null)
                return;
            if (Table.UserS.LoginUser.GetAuthority(DataTableText.Pos机器号) > Table.UserS.EnumAuthority.查看)
            {
                WindowConfig wc = new WindowConfig();
                wc.Init();
                wc.ShowDialog();
            }
            else
                MessageWindow.Show("权限不足");
        }

        private void OpenCashbox()
        {
            if (Table.UserS.LoginUser == null)
                return;
            if (Table.UserS.LoginUser.GetAuthority(Table.PosS.EnumAuthority.弹出钱箱) > Table.UserS.EnumAuthority.无权限)
            {
            }
            else
                MessageWindow.Show("权限不足");
        }

        private void GuaDan()
        {
            if (Table.UserS.LoginUser == null)
                return;
            if (Table.UserS.LoginUser.GetAuthority(Table.PosS.EnumAuthority.挂单) < Table.UserS.EnumAuthority.编辑)
            {
                if (App.Order != null)
                {
                    App.GuaDan.Add(new ClassGuaDan());
                    pSale.GuaDan();
                    pSale.NewOrder();
                    MessageWindow.Show("挂单已保存");
                }
            }
            else
                MessageWindow.Show("权限不足");
        }

        private void NewOrder()
        {
            ISaleTemplete ist = frame1.Content as ISaleTemplete;
            if (ist == null)
                return;

            if (MessageWindow.Show("", "确定取消当前单据吗？", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                ist.NewOrder();
        }

        private void LogOff(bool confirm)
        {
            if (confirm)
            {
                if ((frame1.Content is StoreTemplete.PageLogin))
                    return;
                if (MessageWindow.Show("", "注销当前用户吗？", MessageBoxButton.YesNo) == MessageBoxResult.No)
                    return;
            }

            labelUserName.Content = "未登陆";
            labelSaleMode.Content = "";
            frame1.Navigate(pLogin);
            Table.UserS.LoginUser = null;
        }

        private void CloseThis()
        {
            bool close = true;
            foreach (Window w in this.OwnedWindows)
                if (w.IsActive)
                {
                    w.Close();
                    return;
                }

            if (close)
                this.Close();
        }

        private void ShowHelp()
        {
            WindowHelp wh = new WindowHelp();
            wh.Owner = this;
            wh.ShowDialog();
        }
        #endregion

        internal void Upload()
        {
        }

        internal void Download()
        {
        }
    }

    /// <summary>
    /// 挂单用的
    /// </summary>
    public class ClassGuaDan
    {
        public ClassGuaDan()
        {
            CorSId = App.MWindow.pSale.CorSId;
            VipNO = App.MWindow.pSale.VipNO;
            RemainIntegral = App.MWindow.pSale.RemainIntegral;

            StringBuilder sb = new StringBuilder();
            foreach (Table.ProductS ps in App.Order.ProductSList)
                sb.Append(ps.Id + "," + ps._TempPrice + "," + ps._TempQuantity + ";");

            ProductList = sb.ToString();
        }

        public Table.PosOrderS ToOrder()
        {
            Table.PosOrderS order = new Table.PosOrderS(Table.PosOrderS.EnumFlag.POS零售单) ;
            order.CorSId = CorSId;
            foreach (string str in ProductList.Split(';'))
            {
                if (string.IsNullOrEmpty(str))
                    continue;

                string[] s = str.Split(',');
                Table.ProductS ps = Table.ProductS.GetObject(Convert.ToInt64(s[0]));
                if (ps == null)
                    continue;

                ps._TempPrice = Convert.ToDouble(s[1]);
                ps._TempQuantity = Convert.ToDouble(s[2]);
                order.ProductSList.Add(ps);
            }
            return order;
        }

        public Int64 CorSId { get; set; }
        public string VipNO { get; set; }
        public string RemainIntegral { get; set; }
        public string ProductList { get; set; }
    }
}